using I2.Loc;
using UnityEngine;

public class MarketUpgradeInfoButton : MonoBehaviour
{
	[HideInInspector]
	public MarketProductInfoPanel parentInfoPanel;

	[HideInInspector]
	private Object previewAsset;

	public RotationObject rotator;

	public Transform previewPoint;

	[HideInInspector]
	public GameObject currentPreview;

	[HideInInspector]
	public Product currentProduct;

	public GameObject purchasedIcon;

	public GameObject lockedIcon;

	public UILabel lockedLabel;

	private TweenScale tweenScale;

	private float scaleFactor = 1.4f;

	private float scaleDuration = 0.2f;

	public float previewScale = 150f;

	private Quaternion startRotation;

	public void OnClick()
	{
		parentInfoPanel.SetInfo(currentProduct, false);
		Select(true);
	}

	public void Init(MarketProductInfoPanel parentInfoPanel, Product product)
	{
		if (product != currentProduct)
		{
			tweenScale = GetComponent<TweenScale>();
			if (tweenScale != null)
			{
				tweenScale.duration = scaleDuration;
				tweenScale.from = Vector3.one;
				tweenScale.to = Vector3.one * scaleFactor;
			}
			this.parentInfoPanel = parentInfoPanel;
			currentProduct = product;
			if (parentInfoPanel.currentProduct == product)
			{
				Select(true);
			}
			else
			{
				Select(false);
			}
			startRotation = previewPoint.rotation;
			LoadProductPreview();
		}
	}

	public void Select(bool selected)
	{
		if (selected)
		{
			rotator.enabled = true;
			if (tweenScale != null)
			{
				tweenScale.PlayForward();
			}
		}
		else
		{
			previewPoint.rotation = startRotation;
			rotator.enabled = false;
			if (tweenScale != null)
			{
				tweenScale.PlayReverse();
			}
		}
		if (currentProduct.Purchased)
		{
			purchasedIcon.SetActive(true);
			lockedIcon.SetActive(false);
			return;
		}
		purchasedIcon.SetActive(false);
		bool flag = CupsManager.CurrentTier >= currentProduct.tier;
		League leagueForIndex = CupsManager.GetLeagueForIndex(currentProduct.tier - 1);
		if (!flag)
		{
			lockedLabel.text = string.Format(ScriptLocalization.Get("Market/itemLeagueRequired"), ScriptLocalization.Get("Game_Common/" + leagueForIndex));
		}
		lockedIcon.SetActive(!flag);
	}

	public virtual void LoadProductPreview()
	{
		UnloadProductPreview();
		previewAsset = Resources.Load("ShopGuns/" + currentProduct.id);
		if (previewAsset != null)
		{
			currentPreview = Object.Instantiate(previewAsset) as GameObject;
			currentPreview.transform.SetParent(previewPoint);
			currentPreview.transform.localPosition = Vector3.zero;
			currentPreview.transform.localRotation = Quaternion.identity;
			currentPreview.transform.localScale = Vector3.one * previewScale;
			Transform[] componentsInChildren = currentPreview.GetComponentsInChildren<Transform>();
			foreach (Transform transform in componentsInChildren)
			{
				transform.gameObject.layer = 8;
			}
		}
		else
		{
			BCWDebug.LogError("Cannot load model for shop ShopGuns/" + currentProduct.id);
		}
	}

	public virtual void UnloadProductPreview()
	{
		if ((bool)currentPreview)
		{
			Object.Destroy(currentPreview);
			currentPreview = null;
			previewAsset = null;
		}
	}
}
